package thread;

import java.util.concurrent.atomic.AtomicInteger;

public class ThreadDemo9 {
    //不实用原生的int,而是替换成AtomicInteger
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                //count++
                count.getAndIncrement();//一个指令天生就是原子的
                //++count
                //count.incrementAndGet();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                //count++
                count.getAndIncrement();
                //++count
                //count.incrementAndGet();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }
}
